/**
 * exec() 主要用于配合捕获组使用
 */
;(function () {
    let text = "mom and dad and baby";
    let pattern = /mom( and dad( and baby)?)?/gi

    let matches = pattern.exec(text)
    console.log(matches.index)
    console.log(matches.lastIndexOf())
    console.log(matches[0])
    console.log(matches[1])
    console.log(matches[2])
    console.log("=====================")
})()


/**
 * exec()
 *  如果模式设置了全局标记，则每次调用 exec()方法会返回一个匹配的信息。如果没有设置全局标记，则无论对同一个字符串调用多少次 exec()，也只会返回第一个匹配的信息。
 */
;(function () {
    let text = "cat, bat, sat, fat";
    let pattern = /.at/;
    let matches = pattern.exec(text)
    console.log(matches.index)
    // console.log(matches.input)
    console.log(matches[0])
    console.log(pattern.lastIndex)

    matches = pattern.exec(text)
    console.log(matches.index)
    // console.log(matches.input)
    console.log(matches[0])
    console.log(matches.lastIndex)

})()

/**
 * exec()
 * 如果在这个模式上设置了 g 标记，则每次调用 exec()都会在字符串中向前搜索下一个匹配项
 */
;(function () {
    let text = "cat, bat, sat, fat";
    let pattern = /.at/g;
    let matches = pattern.exec(text);
    console.log(matches.index); // 0
    console.log(matches[0]); // cat
    console.log(matches)
    console.log(pattern.lastIndex); // 3
    matches = pattern.exec(text);
    console.log(matches.index); // 5
    console.log(matches[0]); // bat
    console.log(pattern.lastIndex); // 8
    matches = pattern.exec(text);
    console.log(matches.index); // 10
    console.log(matches[0]); // sat
    console.log(pattern.lastIndex); // 13
    console.log("=================")
})()

/**
 * test()
 * 正则表达式的另一个方法是 test()，接收一个字符串参数。如果输入的文本与模式匹配，则参数返回 true，否则返回 false。这个方法适用于只想测试模式是否匹配，而不需要实际匹配内容的情况。
 */
;(function () {
    let text = "000-00-0000";
    let pattern = /\d{3}-\d{2}-\d{4}/;

    if (pattern.test(text)) {
        console.log("The pattern was matched.");
    }
    console.log("=========")
})()

/**
 * toLocaleString()
 * toString()
 * valueOf()
 */
;(function () {
    let pattern = new RegExp("\\[bc\\]at", "gi");
    console.log(pattern.toString()); // /\[bc\]at/gi
    console.log(pattern.toLocaleString()); // /\[bc\]at/gi
    console.log(pattern.valueOf())  // /\[bc\]at/gi
})()